package com.wy.pdf_test;// 使用 com.microsoft.playwright:playwright-java

import com.wy.pdf_test.service.GenReportPdf;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

@RestController
public class PdfController {
    
    @Resource
    private GenReportPdf genReportPdf;

    @GetMapping("/gen-pdf")
    public String generatePdf() throws IOException {
        File file = genReportPdf.genReportPdf();
        return "✅ PDF 生成成功，路径：" + file.getAbsolutePath();
    }
//        ClassPathResource resource = new ClassPathResource("templates/examReport.html");
//
//        // 拷贝到临时文件
//        File tempFile = File.createTempFile("examReport", ".html");
//        try (InputStream in = resource.getInputStream();
//             OutputStream out = new FileOutputStream(tempFile)) {
//            in.transferTo(out);
//        }
//
//        // 渲染 HTML 为 PDF
//        try (Playwright playwright = Playwright.create()) {
//            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true));
//            Page page = browser.newPage();
//
//            page.navigate(tempFile.toURI().toString());
//
//            // 等待 JS 执行完毕（确保图表渲染完成）
//            page.waitForLoadState(LoadState.NETWORKIDLE);
//            page.waitForTimeout(2000); // 可根据情况调整等待时间
//
//            File output = new File("output/output.pdf");
//            output.getParentFile().mkdirs();
//
//            page.pdf(new Page.PdfOptions()
//                    .setPath(output.toPath())
//                    .setFormat("A4")
//                    .setPrintBackground(true)
//            );
//
//            return "✅ PDF 生成成功，路径：" + output.getAbsolutePath();
//        }
//    }
}
